﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class Pages_Admin_AfiseazaProduse : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ModificaProdus(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Modifica")
        {
            int id = Convert.ToInt32(e.CommandArgument);
            Response.RedirectToRoutePermanent("ModificareProdus", new { ID = id });
        }
        else if (e.CommandName == "Sterge")
        {
            int id = Convert.ToInt32(e.CommandArgument);
            String query = "SELECT deleted FROM Products WHERE ID=@ID";
            SqlConnection connection = new SqlConnection(GetConnectionString()); //deschid conexiune catre baza de date
            connection.Open();
            SqlCommand command = new SqlCommand(query, connection);
            command.Parameters.AddWithValue("@ID", id);

            SqlDataReader reader = command.ExecuteReader();

            if (!reader.Read())
                return;

            String isDeleted = "1";
            if (Convert.ToBoolean(reader["deleted"].ToString()) == true)
                isDeleted = "0";
            reader.Close();
                        
            query = "UPDATE Products SET deleted='" + isDeleted + "' WHERE ID=@ID";
            connection = new SqlConnection(GetConnectionString()); //deschid conexiune catre baza de date
            connection.Open();
            command = new SqlCommand(query, connection);

            command = new SqlCommand(query, connection);
            command.Parameters.AddWithValue("@ID", id);
            command.ExecuteNonQuery();
            connection.Close();
            Response.RedirectToRoutePermanent("AfiseazaProduse");    
        }
    }

    private string GetConnectionString(string connection_name = "StoreDBConnectionString")
    {
        string connection = string.Empty;

        connection = ConfigurationManager.ConnectionStrings[connection_name].ConnectionString;

        return connection;
    }
    
}